package com.zzy.singleton.singleton3;

/**
 * 静态内部类
 * 类加载的机制保证实例化时只有一个线程
 * 静态内部类在它外层类装载时不会立刻实例化，而是需要实例化时再去装载它本身
 */
public class SingletonTest3 {
    public static void main(String[] args) {
        Singleton instance1 = Singleton.getInstance();
        Singleton instance2 = Singleton.getInstance();
        System.out.println(instance1 == instance2);
        System.out.println("instance1.hashCode()>>>" + instance1.hashCode());
        System.out.println("instance2.hashCode()>>>" + instance2.hashCode());
    }
}

class Singleton {

    private Singleton() {
    }

    private static class SingletonInstance {
        private final static Singleton INSTACE = new Singleton();
    }

    public static Singleton getInstance() {
        return SingletonInstance.INSTACE;
    }
}
